package cn.wit.summit.chwan.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import cn.wit.summit.chwan.models.event.AccountChangeEvent;
import cn.wit.summit.chwan.models.event.EnergyEvent;
import cn.wit.summit.chwan.ui.base.MyApplication;
import cn.wit.summit.chwan.ui.receiver.BroadcastAction;

/**
 * Created by pengsk on 2018/4/12.
 */

public class AccountUtil {
    private static final AccountUtil ourInstance = new AccountUtil();

    public static AccountUtil getInstance() {
        return ourInstance;
    }

    private AccountUtil() {
    }


    public int getUid() {
        if (MyApplication.accountBean == null) {
            return 0;
        } else return MyApplication.accountBean.getUid();
    }

    public int getUidNotToLogin(Context context) {
        if (MyApplication.accountBean == null) {
            IntentUtil.getInstance().goLoginActivity(context);
            return 0;
        } else return MyApplication.accountBean.getUid();
    }

    public String getToken() {
        if (MyApplication.accountBean == null) {
            return "";
        } else return MyApplication.accountBean.getToken();

    }

    public AccountBean getAccountData() {
        AccountBean accountBean = MyApplication.getAccountBean();
        if (accountBean == null) {
            SharedPreferences sp = MyApplication.getMyApplication().getShareFer();
            String accountData = sp.getString("accountdata", "");
            AccountBean bean = JsonMapper.getInstance().fromJson(accountData, AccountBean.class);
            MyApplication.accountBean = bean;
            return bean;
        } else return accountBean;
    }

    public void saveAccountData(AccountBean accountBean) {
        SharedPreferences sp = MyApplication.getMyApplication().getShareFer();
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("accountdata", JsonMapper.getInstance().toJson(accountBean));
        editor.commit();
        MyApplication.accountBean = accountBean;
        SharedPreferences sps = MyApplication.getMyApplication().getRedps();
        SharedPreferences.Editor editors = sps.edit();
        editors.putBoolean("isclose", true);
        editors.commit();

        EventBus.getDefault().post(new AccountChangeEvent());
//        Intent intent = new Intent(BroadcastAction.ACTION_USER_INFO_CHANGE);
//        MyApplication.getMyApplication().getApplicationContext().sendBroadcast(intent);
    }

    public void accountLoginOut(Activity context) {
        if (MyApplication.accountBean != null && MyApplication.accountBean.getEnergies() > 0) {
            EnergyEvent event = new EnergyEvent();
            event.setNumber(0);
//            event.setNumber(-MyApplication.accountBean.getEnergies());
            EventBus.getDefault().post(event);
        }
        SharedPreferences sp = MyApplication.getMyApplication().getShareFer();
        MyApplication.accountBean = null;
        sp.edit().clear().commit();
        getUidNotToLogin(context);
    }
}
